import 'package:flutter/material.dart';
import 'package:garbage_classification/global.dart';
import 'package:garbage_classification/pages/classification.dart';
import 'package:garbage_classification/pages/search.dart';
import 'package:titled_navigation_bar/titled_navigation_bar.dart';

class IndexPage extends StatefulWidget {
  IndexPage({Key key}) : super(key: key);
  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  int _currentIndex = 0;
  PageController _pageController;

  @override
  void initState() {
    super.initState();
    _pageController = PageController();
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SizedBox.expand(
        child: PageView(
          controller: _pageController,
          onPageChanged: (index) {
            setState(() => _currentIndex = index);
          },
          children: <Widget>[
            SearchPage(),
            ClassificationPage(),
          ],
        ),
      ),
      bottomNavigationBar: TitledBottomNavigationBar(
        currentIndex: _currentIndex,
        reverse: true,
        enableShadow: false,
        activeColor: Global.mainColor,
        onTap: (index) {
          setState(() => _currentIndex = index);
          _pageController.jumpToPage(index);
        },
        items: [
          TitledNavigationBarItem(
            title: Text(
              '首页',
              style: TextStyle(
                color: Global.mainColor,
                fontSize: 13,
                fontWeight: FontWeight.w600,
              ),
            ),
            icon: Icons.home,
          ),
          TitledNavigationBarItem(
            title: Text(
              '分类百科',
              style: TextStyle(
                color: Global.mainColor,
                fontSize: 13,
                fontWeight: FontWeight.w600,
              ),
            ),
            icon: Icons.book,
          ),
        ],
      ),
    );
  }
}
